package com.example.blog_system_ssm.controller;

import com.example.blog_system_ssm.common.AjaxResult;
import com.example.blog_system_ssm.common.Constant;
import com.example.blog_system_ssm.common.SensitiveWordUtil;
import com.example.blog_system_ssm.common.UserThreadLocal;
import com.example.blog_system_ssm.model.ArticleInfo;
import com.example.blog_system_ssm.model.UserInfo;
import com.example.blog_system_ssm.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/art")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private SensitiveWordUtil sensitiveWordUtil;
    @RequestMapping("/mylist")
    public List<ArticleInfo> myList(Integer pindex, Integer psize){
        UserInfo userInfo= UserThreadLocal.get();
        if(userInfo!=null){
            if (pindex == null || psize == null) {
                return null;
            }
            //分页公式
            int offset=(pindex-1)*psize;
            return articleService.getMyList(userInfo.getId(),psize,offset);
        }
        return null;
    }

    @RequestMapping("/artdel")
    public Object artDel(Long aid){
        UserInfo userInfo= UserThreadLocal.get();
        if(userInfo!=null){
            if(articleService.delete(aid,userInfo.getId())==1){
                return AjaxResult.success("删除成功");
            }
        }
        return AjaxResult.fail(-1,"","删除失败，文章不存在");
    }

    @RequestMapping("/detailbyid")
    public Object getDetilById(Long aid){
        if(aid!=null&&aid>0){
            //获取文章详情
            ArticleInfo articleInfo=articleService.getDetail(aid);
            UserInfo userInfo=UserThreadLocal.get();
            //验证文章归属人
            if(userInfo!=null&&articleInfo!=null&&articleInfo.getUid()==userInfo.getId()){
                return AjaxResult.success(articleInfo);
            }
        }
        return AjaxResult.fail(-1,"查询失败");
    }

    @RequestMapping("/update")
    public Object update(Long aid, String title, String content){
        UserInfo userInfo=UserThreadLocal.get();
        //敏感词屏蔽
        if(sensitiveWordUtil.contains(title)||sensitiveWordUtil.contains(content)){
            title=sensitiveWordUtil.replaceSensitiveWord(title,'*',2);
            content=sensitiveWordUtil.replaceSensitiveWord(content,'*',2);
        }
        if(userInfo!=null&&userInfo.getId()>0&&aid>0){
            return articleService.update(aid,userInfo.getId(),title,content);
        }
        return -1;
    }

    @RequestMapping("/add")
    public int add(String title,String content){
        UserInfo userInfo=UserThreadLocal.get();
        //敏感词屏蔽
        if(sensitiveWordUtil.contains(title)||sensitiveWordUtil.contains(content)){
            title=sensitiveWordUtil.replaceSensitiveWord(title,'*',2);
            content=sensitiveWordUtil.replaceSensitiveWord(content,'*',2);
        }
        if(userInfo!=null&&userInfo.getId()>0){
            return articleService.add(userInfo.getId(),title,content);
        }
        return -1;
    }


    @RequestMapping("/detail")
    public Object getDetail(HttpServletRequest request,Long aid){
        if(aid>0){
            ArticleInfo articleInfo=articleService.getDetail(aid);
            if(articleInfo!=null){
                //写入redis记录增添的阅读量
                redisTemplate.opsForHyperLogLog().add(Constant.RCOUNT_ARTICLE_ID+aid,request.getSession().getAttribute(Constant.SESSION_USERIP_KEY));
                return articleInfo;
            }
        }
        return AjaxResult.fail(-1,"查询失败");
    }

    @RequestMapping("/totalpage")
    public int totalPage(Integer psize){
        if(psize>0){
            int totalCount = articleService.getTotalCount();
            //计算总页数
            int totalPage= (int) Math.ceil(totalCount*1.0/psize);
            return totalPage;
        }
        return 0;
    }

    @RequestMapping("/mytotalpage")
    public int myTotalPage(Integer psize){
        UserInfo userInfo=UserThreadLocal.get();
        if(psize>0&&userInfo!=null){
            int totalCount = articleService.getMyTotalCount(userInfo.getId());
            //计算总页数
            int totalPage= (int) Math.ceil(totalCount*1.0/psize);
            return totalPage;
        }
        return 0;
    }

    @RequestMapping("/list")
    public List<ArticleInfo> getList(Integer pindex, Integer psize){
        if (pindex == null || psize == null) {
            return null;
        }
        //分页公式
        int offset=(pindex-1)*psize;
        return articleService.getList(psize,offset);
    }
}
